/*
 * Remove Duplicates from Sorted Array II
 * duplicates are allowed at most twice
 */

#include "../../func.h"

int rmvDuplicates1(vector<int> &nums)
{
    if (nums.size() <= 2)
        return nums.size();

    int idx = 2;
    for (int i = 2; i < nums.size(); ++i)
    {
        if (nums[i] != nums[idx - 2])
            nums[idx++] = nums[i];
    }
    return idx;
}

int rmvDuplicates2(vector<int> &nums)
{
    const int n = nums.size();
    int idx = 0;
    for (int i = 0; i < n; ++i)
    {
        if (i > 0 && i < n - 1 && nums[i] == nums[i - 1] && nums[i] == nums[i + 1])
            continue;
        nums[idx++] = nums[i];
    }
    return idx;
}